/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * buscaPlanoPagamento.java
 *
 * Created on 24/08/2009, 20:34:24
 */

package siga.visao_busca;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import siga.banco.conexao;
import siga.visao.index;
import siga.visao.verErros;

/**
 *
 * @author rafaelfrasson
 */
public class buscaPlanoPagamento extends javax.swing.JDialog {

    /** Creates new form buscaPlanoPagamento */
    public buscaPlanoPagamento(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jtfCodigo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jtfDescicao = new javax.swing.JTextField();
        jbBusca = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtPlano = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Código:");
        jLabel1.setName("jLabel1"); // NOI18N

        jtfCodigo.setName("jtfCodigo"); // NOI18N
        jtfCodigo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfCodigoKeyPressed(evt);
            }
        });

        jLabel2.setText("Plano de Pagamento:");
        jLabel2.setName("jLabel2"); // NOI18N

        jtfDescicao.setName("jtfDescicao"); // NOI18N
        jtfDescicao.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtfDescicaoKeyPressed(evt);
            }
        });

        jbBusca.setIcon(new javax.swing.ImageIcon(getClass().getResource("/siga/imagem/busca_16x16.png"))); // NOI18N
        jbBusca.setMargin(new java.awt.Insets(2, 2, 2, 2));
        jbBusca.setName("jbBusca"); // NOI18N
        jbBusca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbBuscaActionPerformed(evt);
            }
        });

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jtPlano.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Plano de Pagamento"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtPlano.setName("jtPlano"); // NOI18N
        jtPlano.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtPlanoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtPlano);
        jtPlano.getColumnModel().getColumn(0).setPreferredWidth(100);
        jtPlano.getColumnModel().getColumn(0).setMaxWidth(100);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jtfDescicao, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbBusca)))
                .addContainerGap())
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jtfCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfDescicao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jbBusca))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbBuscaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbBuscaActionPerformed
        busca();
    }//GEN-LAST:event_jbBuscaActionPerformed

    private void jtPlanoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtPlanoMouseClicked
        if(evt.getClickCount()==2){
            javax.swing.table.DefaultTableModel jtp = (javax.swing.table.DefaultTableModel) jtPlano.getModel();
            index.LRplanopag.add(0, jtp.getValueAt(jtPlano.getSelectedRow(), 0));
            index.LRplanopag.add(1, jtp.getValueAt(jtPlano.getSelectedRow(), 1));
            dispose();
        }
    }//GEN-LAST:event_jtPlanoMouseClicked

    private void jtfCodigoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfCodigoKeyPressed
        if(evt.getKeyCode()==10){
            busca();
        }
    }//GEN-LAST:event_jtfCodigoKeyPressed

    private void jtfDescicaoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfDescicaoKeyPressed
        if(evt.getKeyCode()==10){
            busca();
        }
    }//GEN-LAST:event_jtfDescicaoKeyPressed

    private void busca(){
        try {
            String cod = (jtfCodigo.getText().equals("")) ? "" : " and codigo=" + jtfCodigo.getText();
            String des = (jtfDescicao.getText().equals("")) ? "" : " and descricao like \"" + jtfDescicao.getText() + "%\"";
            conexao.Query("select codigo, descricao from fin_plano_pagamento where cod_emp=" + index.cod_emp + "" + cod + des);
            javax.swing.table.DefaultTableModel jtp = (javax.swing.table.DefaultTableModel) jtPlano.getModel();
            for (int x = jtPlano.getRowCount() - 1; x >= 0; --x) {
                jtp.removeRow(x);
            }
            while (conexao.rs.next()) {
                jtp.addRow(new Object[]{conexao.rs.getString("codigo"),conexao.rs.getString("descricao")});
            }
        } catch (SQLException ex) {
            verErros.setErro(ex.getMessage());
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbBusca;
    private javax.swing.JTable jtPlano;
    private javax.swing.JTextField jtfCodigo;
    private javax.swing.JTextField jtfDescicao;
    // End of variables declaration//GEN-END:variables

}
